package edu.sctu.demo;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by admin on 2018/6/14.
 */
public class LoginFilter implements Filter {
    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig=filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        HttpSession session=request.getSession();
        String isLogin="";
        isLogin=(String)session.getAttribute("islogin");
        if (isLogin==null){
            response.sendRedirect("login.jsp");
            return;
        }
        if (isLogin.equals("true")){
            filterChain.doFilter(request,response);
        }else {
            response.sendRedirect("login.jsp");
            return;
        }
    }

    @Override
    public void destroy() {
        this.filterConfig=null;
    }
}

